package com.group.EducationalSystem.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.druid.sql.visitor.functions.If;
import com.group.EducationalSystem.bean.Course;
import com.group.EducationalSystem.bean.Exam;
import com.group.EducationalSystem.bean.Score_tab;
import com.group.EducationalSystem.bean.Student;
import com.group.EducationalSystem.bean.StudentScoreMsg;
import com.group.EducationalSystem.bean.TeacherSelfMsg;
import com.group.EducationalSystem.mapper.ExamMapper;
import com.group.EducationalSystem.mapper.ScoreMapper;
import com.group.EducationalSystem.mapper.TeacherMapper;
import com.mysql.cj.Session;
import com.mysql.cj.xdevapi.Result;

@Controller
public class TeacherController {
	@Resource
	TeacherMapper teacherMapper;
	@Resource
	ExamMapper examMapper;
	@Resource
	ScoreMapper scoreMapper;
	
	public boolean checkLoginStatus(HttpSession session) {
		if(session.getAttribute("teacherInfo") != null) {
			return true;
		}
		else {
			return false;
		}
	}
	
	@RequestMapping("/gotoTeacherPage")
	public String gotoTeacherPage(ModelMap m, HttpSession session){
//		验证登录状态
		if(checkLoginStatus(session)) {
			TeacherSelfMsg teacherInfo = (TeacherSelfMsg) session.getAttribute("teacherInfo");
			m.put("teacherInfo", teacherInfo);
			m.put("nowPage", "teacherMsgPage");
			return "TemplateTeacher";
		}
		else {
			return "login";
		}
	}
	
	@RequestMapping("/gotoModifyTeacherMsg")
	public String gotoModifyTeacherMsg(ModelMap m, HttpSession session) {
//		TeacherSelfMsg teacherInfo = TeacherMapper.getTeacherMsgByNumber();
//		System.out.println(teacherInfo.toString());
		if(checkLoginStatus(session)) {
			TeacherSelfMsg teacherInfo = (TeacherSelfMsg) session.getAttribute("teacherInfo");
			m.put("teacherInfo", teacherInfo);
			m.put("nowPage", "teacherModifyMsgPage");
			return "TemplateTeacher";
		}
		else {
			return "login";
		}
	}
	
	@RequestMapping("/modifyTeacherInfo")
	public String modifyTeacherInfo(ModelMap m, HttpSession session, TeacherSelfMsg teacherSelfMsg) {
//		
		teacherSelfMsg.setTeacher_id(((TeacherSelfMsg)session.getAttribute("teacherInfo")).getTeacher_id());
		System.out.println("传入teacher info " + teacherSelfMsg);
		int result = teacherMapper.updateTeacherInfoById(teacherSelfMsg);
		System.out.println(result);
		TeacherSelfMsg teacherSelfMsg2 = new TeacherSelfMsg();
		
		
		
//		teacherSelfMsg = teacherMapper.getTeacherMsgByNumber();
		m.put("teacherInfo", teacherSelfMsg);
		System.out.println("再次查询的 teacher info " + teacherSelfMsg);
		session.setAttribute("teacherInfo", teacherSelfMsg);
		m.put("flag", "modifyTeacherMsgSuccessful");
		m.put("nowPage", "teacherMsgPage");
		return "TemplateTeacher";
	}
	
	@RequestMapping("/addStudentScore")
	public String addStudentScore(ModelMap m, HttpSession session) {
		Student [] students = teacherMapper.getStudentByCollegeId(((TeacherSelfMsg)session.getAttribute("teacherInfo")).getCollege_id());
		m.put("studentInfo", students);
		m.put("nowPage", "addStudentScore");
		return "TemplateTeacher";
	}
	

	/*
	 * 跳转添加考试页面
	 * session登录验证
	 */
	@RequestMapping("/gotoAddExam")
	public String gotoAddExam(ModelMap m, HttpSession session) {
		if(checkLoginStatus(session)) {
			m.put("nowPage", "AddExam");
			return "TemplateTeacher";
		}
		else {
			return "login";
		}
		
	}
	/*
	 * 添加考试，每个老师只能同时拥有一个考试
	 * session登录验证
	 */
	@RequestMapping("/addExam")
	public String addExam(Exam exam, ModelMap m, HttpSession session) {
		if(checkLoginStatus(session)) {
			System.out.println(exam);
//			按专业插入考试数据，每个老师只能有一个考试
//			验证是否已经有考试
			Exam e1 = new Exam();
			e1.setCourse_id(((TeacherSelfMsg)session.getAttribute("teacherInfo")).getCollege_id());
			if(examMapper.getExam(e1) != null) {
//				提示已经有一个考试了
				m.put("nowPage", "AddExam");
				m.put("flag", "hadExam");
				return "TemplateTeacher";
			}
			else {
//				根据专业添加考试
				exam.setCourse_id(((TeacherSelfMsg)session.getAttribute("teacherInfo")).getCollege_id());
				examMapper.addExam(exam);
				m.put("nowPage", "AddExam");
				m.put("flag", "addExamsuccessful");
				return "TemplateTeacher";
			}
		}
		else {
			return "login";
		}
	}
	/*
	 * 跳转修改考试信息页面
	 */
	@RequestMapping("/modifyExam")
	public String modifyExam(ModelMap m, HttpSession session) {
		if(checkLoginStatus(session)) {
			Exam e1 = new Exam();
			e1.setCourse_id(((TeacherSelfMsg)session.getAttribute("teacherInfo")).getCollege_id());
			
			Exam exam = examMapper.getExam(e1);
			System.out.println("======================++++++++++++"+exam);
			m.put("exam", exam);
			m.put("nowPage", "modifyExam");
			return "TemplateTeacher";
		}
		else {
			return "login";
		}
		
	}
	/*
	 * 处理修改考试信息请求
	 */
	@RequestMapping("/upDateModifyExam")
	public String upDateModifyExam(ModelMap m, Exam exam, HttpSession session) {
		if (checkLoginStatus(session)) {
			int course_id = ((TeacherSelfMsg)session.getAttribute("teacherInfo")).getCollege_id();
			exam.setCourse_id(course_id);
			examMapper.modifyExam(exam);
			exam = examMapper.getExam(exam);
			m.put("exam", exam);
			m.put("nowPage", "lookExam");
			m.put("flag", "modifyExamSuccessful");
			return "TemplateTeacher";
		}
		else {
			return "login";
		}
	}
	/*
	 * 查看已经发布的考试信息
	 */
	@RequestMapping("/lookExam")
	public String lookExam(ModelMap m, HttpSession session) {
		if(checkLoginStatus(session)) {
//			检查是否有考试
			Exam e1 = new Exam();
			e1.setCourse_id(((TeacherSelfMsg)session.getAttribute("teacherInfo")).getCollege_id());
			Exam exam = examMapper.getExam(e1);
			if(exam != null) {
				m.put("exam", exam);
				m.put("nowPage", "lookExam");
				return "TemplateTeacher";
			}
			else {
				m.put("flag", "noExam");
				m.put("nowPage", "lookExam");
				return "TemplateTeacher";
			}
		}
		else {
			return "login";
		}
	}
//	获取学生分数用于编辑
	
	@RequestMapping("/getStudentScore")
	@ResponseBody
	public List<Score_tab> getStudentScore() {
		List<Score_tab> list = scoreMapper.getClassStudentScore("软件工程");
		return list;
	}
	/*
	 * 查看课程信息
	 */
	@RequestMapping("/lookClassMessage")
	public String lookClassMessage(ModelMap m, HttpSession session) {
		if(checkLoginStatus(session)) {
			Course [] courseInfo = teacherMapper.getClassMsgByTeacherId(((TeacherSelfMsg)session.getAttribute("teacherInfo")).getTeacher_id());
			System.out.println(courseInfo.toString());
			for(Course c:courseInfo) {
				System.out.println(c.toString());
			}
			m.put("courseInfo", courseInfo);
			m.put("nowPage", "lookClassMessage");
			return "TemplateTeacher";
		}
		else {
			return "login";
		}

	}
	
    @RequestMapping("/gotoAddClassMessage")
    public String gotoAddClassMessage(ModelMap m, HttpSession session) {
    	if(checkLoginStatus(session)) {
    		m.put("nowPage", "addClassPage");
    		return "TemplateTeacher";
    	}
    	else {
    		return "login";
    	}
    }
//    
    @RequestMapping("/addClassMessage")
    public String addClassMessage(ModelMap m, HttpSession session, Course c) {
    	if (checkLoginStatus(session)) {
			System.out.println(c.getCourse_name() + "  " + c.getCourse_time());
			c.setCollege_id(((TeacherSelfMsg)session.getAttribute("teacherInfo")).getCollege_id());
			c.setTeacher_id(((TeacherSelfMsg)session.getAttribute("teacherInfo")).getTeacher_id());
			c.setTeacher_name(((TeacherSelfMsg)session.getAttribute("teacherInfo")).getTeacher_name());
			teacherMapper.addCourse(c);
			Course [] courseInfo = teacherMapper.getClassMsgByTeacherId(((TeacherSelfMsg)session.getAttribute("teacherInfo")).getTeacher_id());
			System.out.println(courseInfo.toString());
			m.put("flag", "addClassSuccessful");
			m.put("courseInfo", courseInfo);
			m.put("nowPage", "lookClassMessage");
			return "TemplateTeacher";
		}
    	else {
    		return "login";
    	}
    }
//    
    @RequestMapping("/gotoModifyClassMessage")
    public String gotoModifyClassMessage(ModelMap m, HttpSession session) {
    	if(checkLoginStatus(session)) {
    		m.put("nowPage", "modifyClassMessage");
    		return "TemplateTeacher";
    	}
    	else {
    		return "login";
    	}
    }
    
    @RequestMapping("/getClassMessage")
    @ResponseBody
    public List<Course> getClassMessage(ModelMap m, HttpSession session){
    	Course [] courseInfo = teacherMapper.getClassMsgByTeacherId(((TeacherSelfMsg)session.getAttribute("teacherInfo")).getTeacher_id());
    	List<Course> list = List.of(courseInfo);
    	return list;
    }
    
    @RequestMapping("/deleteCourse")
    public void deleteCourse(@RequestParam("course_id")int course_id) {
    	teacherMapper.deleteCourseById(course_id);
    	System.out.println("删除成功");
    }
    
    @RequestMapping("/modifyCourse")
    @ResponseBody
    public int modifyCourse(@RequestParam("course_id") int course_id, 
    		@RequestParam("course_name") String course_name,
    		@RequestParam("course_time") String course_time) {
    	Course course = new Course();
    	course.setCourse_id(course_id);
    	course.setCourse_name(course_name);
    	course.setCourse_time(course_time);
    	teacherMapper.updateCourse(course);
    	System.out.println("课程信息修改成功");
    	return 1;
    }
    
    @RequestMapping("/lookStudentScore")
    public String lookStudentScore(ModelMap m, HttpSession s) {
    	m.put("nowPage", "lookStudentScore");
    	return "TemplateTeacher";
    }
    
    @RequestMapping("/getStudentScoreMessage2")
    @ResponseBody
    public List<StudentScoreMsg> getStudentScoreMessage2(HttpSession session){
    	int collegeId = ((TeacherSelfMsg)session.getAttribute("teacherInfo")).getCollege_id();
    	StudentScoreMsg scoreMsg[] = teacherMapper.getStudentScoreByCollegeId(collegeId);
    	List<StudentScoreMsg> list = List.of(scoreMsg);
    	System.out.println(list);
    	return list;
    }
    
    @RequestMapping("/modifyAndAddStudentScore")
    public String modifyAndAddStudentScore(HttpSession session, ModelMap m) {
    	if(checkLoginStatus(session)) {
    		
    		m.put("nowPage", "modifyAndAddStudentScore");
    		return "TemplateTeacher";
    	}
    	else {
    		return "login";
    	}
    }
    
    @RequestMapping("/getStudentScoreMessage")
    @ResponseBody
    public List<StudentScoreMsg> getStudentScoreMessage(HttpSession session){
    	int collegeId = ((TeacherSelfMsg)session.getAttribute("teacherInfo")).getCollege_id();
    	StudentScoreMsg scoreMsg[] = teacherMapper.getStudentScoreByCollegeId(collegeId);
    	List<StudentScoreMsg> list = List.of(scoreMsg);
    	System.out.println(list);
    	return list;
    }
    
    @RequestMapping("/modifyScore")
    public void modifyScore(@RequestParam("score") String score,@RequestParam("student_id") String student_id) {
    	System.out.println("更新分数==================================");
    	scoreMapper.updateScoreByStudentId(score, Integer.parseInt(student_id));
    	System.out.println("分数更新成功");
    }
//    
//    @RequestMapping("/modifyClassMessage")
//    public String modifyClassMessage() {
//    	
//    }
    
}
